package com.machine.filling.db.dao;


import com.machine.filling.MyApp;
import com.machine.filling.db.greendao.CardTempModleDao;
import com.machine.filling.db.modle.CardTempModle;

import java.util.List;

/**
 * Created by Administrator on 2018/4/1 0001.
 */

public class CardTempDao {
    private static CardTempDao instance;
    private static CardTempModleDao cardTempModleDao;
    private static CardTempModle cardTempModle;

    private CardTempDao() {
    }

    public static CardTempDao getInstance() {
        if (instance == null) {
            instance = new CardTempDao();
            cardTempModleDao = MyApp.getInstance().getDaoSession().getCardTempModleDao();
            cardTempModle = new CardTempModle();
        }
        return instance;
    }

    /**
     * 通过端口查看油卡
     *
     * @param portNum
     * @return
     */
    public CardTempModle queryByPortNum(String portNum) {
        List<CardTempModle> modles = cardTempModleDao.queryBuilder().where(CardTempModleDao.Properties.PortNum
                .eq(portNum)).list();
        return modles == null || modles.size() == 0 ? null : modles.get(0);
    }

    public void insert(CardTempModle cardTempModle) {
        cardTempModleDao.insert(cardTempModle);
    }

    public void update(CardTempModle cardTempModle){
        CardTempModle card = queryByPortNum(cardTempModle.getPortNum());
        if (card != null){
            cardTempModle.setAutoId(card.getAutoId());
            cardTempModleDao.update(cardTempModle);
        }else {
            insert(cardTempModle);
        }
    }

    public void deleteByPortNum(String portNum) {
        CardTempModle modle = queryByPortNum(portNum);
        if (modle != null) {
            cardTempModleDao.delete(modle);
        }
    }

    public void deleteAll() {
        cardTempModleDao.deleteAll();
    }
}
